package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabManufacturer;
import com.ysd.lis.mapper.lab.LabManufacturerMapper;
import com.ysd.lis.request.qc.ManufacturerDto;
import com.ysd.lis.request.qc.ManufacturerPageParam;
import com.ysd.lis.service.lab.LabManufacturerService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-01-31
 */
@Service
public class LabManufacturerServiceImpl extends ServiceImpl<LabManufacturerMapper, LabManufacturer> implements LabManufacturerService {
    @Autowired
    LabManufacturerMapper labManufacturerMapper;
    @Override
    public Result findManufacturerPageList(ManufacturerPageParam pageParam) {
        Page<LabManufacturer> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<LabManufacturer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabManufacturer::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabManufacturer::getManuName, pageParam.getSeachValue())
                    .or().like(LabManufacturer::getManuCode, pageParam.getSeachValue())
                    .or().like(LabManufacturer::getInputCode,pageParam.getSeachValue()));
        }

        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(LabManufacturer::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(LabManufacturer::getSeq);
        Page<LabManufacturer> manufacturerPage = labManufacturerMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", manufacturerPage);
    }

    @Override
    public Result addManufacturer(LabManufacturer manufacturer) {
        int insert = labManufacturerMapper.insert(manufacturer);
        if(insert>0){
            return Result.succ(1,"添加成功",manufacturer);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editManufacturer(LabManufacturer manufacturer) {
        int updateById = labManufacturerMapper.updateById(manufacturer);
        if(updateById>0){
            return Result.succ(1,"提交成功",manufacturer);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteManufacturer(LabManufacturer manufacturer) {
        int deleteById = labManufacturerMapper.deleteById(manufacturer.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteManufacturerList(ManufacturerDto dto) {
        List<LabManufacturer> manufacturerList = dto.getManufacturerList();
        List<String> ids = manufacturerList.stream().map(LabManufacturer::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
